<?php
	class ManagerAction extends BaseAction{
		public function index()
		{
			$manager = M("Manager");
			import("ORG.Util.Page");
			$count = $manager->count();
			$page = new Page($count,C('PAGESIZE'));
			$show = $page->show();
			$data = $manager->limit($page->firstRow.','.$page->listRows)
							->field("zgcms_manager.id,name,login_count,last_ip,last_time,zgcms_level.level_name")
							->join("zgcms_level ON zgcms_manager.level = zgcms_level.id")
							->select();
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}
		
		public function add(){
			$level = M("level");
			$data = $level->field("id,level_name")->select();
			$this->assign('level',$data);
			$this->display();
		}
		
		public function insert(){
			$manager = D("Manager");
			if($manager->create())
			{
				if($manager->add())
				{
					$this->assign("jumpUrl","__URL__");
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($manager->getError());
			}			
		}
		
		public function edit()
		{
			if(!empty($_GET['id']))
			{
				$manager = M("Manager");
				$data = $manager->getById($_GET['id']);
// 				var_dump($data);
				if(null == $data)
					$this->error("请确认要修改的管理员ID合法！");
				$this->assign('manager',$data);
				$level = M("Level");
				$this->assign('level',$level->field("id,level_name")->select());
				$this->display();
				
			}else{
				$this->error("请选择要修改的管理员！");
			}
		}
		
		public function update()
		{
			$manager = D("Manager");	
			if($manager->create()){
				if($manager->save()){
					$this->success("更新成功！");
				}else{
					$this->error("因为数据没变化或更新失败！");
				}
			}else{
				$this->error($manager->getError());
			}
		
		}
		
		public function delete()
		{
			if(!empty($_GET['id']))
			{
				$manager = M("Manager");
				if($manager->delete($_GET['id'])){
					$this->success("删除管理员成功!");
				}else{
					$this->error("删除管理员失败!");
				}
			}else{
				$this->error("请选择要删除的管理员！");
			}
		}
	}
?>